<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	    
class Bahan_masuk extends Member_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library(array('table','form_validation','server_processing'));
		$this->load->helper(array('form','url','tgl_indonesia'));
		$this->load->model('bahan_masuk_model','',TRUE);
		$this->load->model('data_gudang_model','',TRUE);
	}
	
	function cek(){
		$id = $this->input->post('id_bahan');
		if(!empty($id)){
			$check=$this->data_gudang_model->cek_id($id);
			if($check->num_rows == 0)
				echo "true";
			else
				echo "false";
		}
		else
			echo "false";
	}
	
	function index(){
		$count = $this->bahan_masuk_model->count_all();
		$count_temp = $this->bahan_masuk_model->count_temp_all();
		
		if($count_temp==0)$last=1;
		else
			$last = $this->bahan_masuk_model->get_last()->row()->no_transaksi;
			
		if($count>0)
			$last_no = $last+1;
		else
			$last_no = $last;
				
		if (isset($_POST['add'])) {
		$id_bahan = $this->data_gudang_model->get_id_bahan($this->input->post('nama_bahan'))->row();
			$bahan_masuk=array(
			'no_transaksi'=>$last_no,
			'id_bahan'=>$id_bahan->id_bahan,
			'tanggal'=>$this->input->post('tanggal'),
			'jumlah'=>$this->input->post('jumlah'));
			//input ke db
			$this->bahan_masuk_model->save($bahan_masuk);
		}
		else if (isset($_POST['save'])) {
			$bahan_masuk=array(
			'tanggal'=>$this->input->post('tanggal'),
			'jumlah'=>$this->input->post('jumlah'));
			//update ke db
			$this->bahan_masuk_model->update($this->input->post('id'),$bahan_masuk);
		}
		else if (isset($_POST['delete'])) {
			$this->bahan_masuk_model->delete($this->input->post('id'));
		}
		else if (isset($_POST['selesai'])) {
			$this->bahan_masuk_model->fix_save($last_no);
		}
	$partial['_header']='template/header';
	$partial['_sidebar']="template/sidebar";
	$partial['_contents']="bahan_masuk/bahan_masuk";
		
	// set common properties
	$data['title'] = 'Bahan Masuk';
	$data['action'] = site_url('bahan_gudang/add/do');
	$data['form_attr'] = array('class' => 'form-horizontal', 'id' => 'FormGudang');
	$data['link_back'] = anchor('data_gudang','Back to list of data gudang',array('class'=>'back'));
	$data['disable'] = '';
//	$this->load->model('data_gudang_model','',TRUE);
	$data['bahan_gudang']=$this->data_gudang_model->data_getall()->result();
	$data['jumlah_bahan_gudang']=$this->data_gudang_model->getName()->num_rows();
	$data['nama_bahan_gudang']=$this->data_gudang_model->getName()->result();
	$data['tgl'] = nama_hari(date("Y-m-d")).', '. tgl_indo(date("Y-m-d"));
	$this->template->load('index',$partial,$data);

	}	
	function _set_rules(){
	$this->form_validation->set_rules('id_bahan','Id','required');
	}
	
	function server(){
	/* ------inisialisasi tabel------- */
	//jika tidak di inisialisasi isi "", kecuali nama table wajib di isi
	//jika menggunakan join, nama field harus lengkap dengan tabelnya kecuali nama fieldnya uniq
	$field = array('id_bahanmasuk','nama_bahan','temp_bahan_masuk.jumlah');
	$table_name = "temp_bahan_masuk";
	$uri = $this->uri->segment(1);
	$join = array("bahan_gudang");
	$on = array("bahan_gudang.id_bahan=temp_bahan_masuk.id_bahan");
	$init = array('select' => $field, 'table_name' => $table_name,'join' => $join, 'on' => $on, 'uri' => $uri);
	$this->server_processing->server_data($_GET,$init);	
	}
	function report($last)
	{
		$this->bahan_masuk_model->fix_save();
		
		foreach($this->bahan_masuk_model->distinct_table()->result() as $rows){
			$row = $this->bahan_masuk_model->jumlah($rows->id_bahan);
			$id=$rows->id_bahan;
			$bahan=array('stok'=>$rows->stok+$row['jumlah']);	
			$this->data_gudang_model->update($id,$bahan);
		}
		$this->bahan_masuk_model->delete_all();
		
		$this->load->library('cezpdf');
		$this->load->helper('pdf');
		 
		prep_pdf(); // creates the footer for the document we are creating.
		$no = 1;
		
		foreach($this->bahan_masuk_model->getAll($last)->result() as $rows){
		$db_data[] = array('no' => $no++, 'nama_bahan' => $rows->nama_bahan, 'jumlah' => $rows->stok);
		}
		$col_names = array(
			'no' => 'Number',
			'nama_bahan' => 'Nama Bahan',
			'jumlah' => 'Jumlah'
		);
		$this->cezpdf->ezTable($db_data, $col_names, 'Bahan Masuk', array('width'=>550));
		$this->cezpdf->ezStream();
	}
}